package global;

import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;

/**@author A0105661M*/
public class Methods {
	private static final int NON_EXISTENT = -1;
	
	
	public static String removeFirstWord(String userCommand) {
		String firstWord = getFirstWord(userCommand);
		return userCommand.substring(firstWord.length()).trim();
	}

	public static String getFirstWord(String userCommand) {
		String commandTypeString = userCommand.trim().split("\\s+")[0];
		return commandTypeString;
	}

	public static String[] stringToArray(String userCommand) {
		String[] stringArray = userCommand.trim().split("\\s+");
		return stringArray;
	}
	
	public static String StringArrayToString(String[] StringArray) {
		StringBuilder builder = new StringBuilder();
		for (String s : StringArray) {
			builder.append(s);
			builder.append(Constant.SPACE);
		}
		return builder.toString().trim();
	}
	
	public static int safeParseInt(String string) {
		Integer i = NON_EXISTENT;
		
		try{
			i = Integer.parseInt(string);
		} catch (Exception e){
			return NON_EXISTENT;
		}
		return i;
	}
	
	public static int[] mySorting(int[] startingIndexOfDivisionInStrictFormat) {
		int[] newIndexArray = startingIndexOfDivisionInStrictFormat.clone();
		Arrays.sort(newIndexArray);
		return newIndexArray;
	}
	
	public static int findInArray(int[] array, int value) {
		for (int i = 0; i < array.length; i++) {
			if (array[i] == value)
				return i;
		}
		return -1;
	}
	
	public static String stringArrayToString(String[] stringArray, int start, int end){
		StringBuilder builder = new StringBuilder();
		for(int i=start; i<end; i++) {
		    builder.append(stringArray[i]);
		    builder.append(" ");
		}
		return builder.toString();
	}
	
	public static ArrayList<String> appendStringList(
			ArrayList<String> list1,
			ArrayList<String> list2,
			ArrayList<String> list3,
			ArrayList<String> list4) {
    	ArrayList<String> newFilenameList = new ArrayList<String>();
    	
    	if (list1 != null){
	    	for (int i=0; i<list1.size(); i++){
	    		newFilenameList.add(list1.get(i));
	    	}
    	}
    	if (list2 != null){
	    	for (int i=0; i<list2.size(); i++){
	    		newFilenameList.add(list2.get(i));
	    	}
    	}
    	if (list3 != null){
    		for (int i=0; i<list3.size(); i++){
        		newFilenameList.add(list3.get(i));
        	}
    	}
    	if (list4 != null){
	    	for (int i=0; i<list4.size(); i++){
	    		newFilenameList.add(list4.get(i));
	    	}
    	}
		return newFilenameList;
	}
	
	public static void transerIntArray(int[] sender, int senderStartIndex, 
			int[] receiver, int receiverStartIndex, int length){
		for (int i=0; i<length; i++){
			receiver[receiverStartIndex+i] = sender[senderStartIndex+i]; 
		}	
	}
	
	public static int findMax(int[] array) {
		int max = -1;
		for (int i = 0; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
		}
		return max;
	}

	public static int findMin(int[] array) {
		int min = -1;
		for (int i = 0; i < array.length; i++) {
			if ((array[i] >= 0 && array[i] < min) || min < 0) {
				min = array[i];
			}
		}
		return min;
	}
	
	/** Open a browser at the given URL. */
	/** @author 2011 Google Inc */
	public static void browse(String url) {
	    // first try the Java Desktop
	    if (Desktop.isDesktopSupported()) {
	      Desktop desktop = Desktop.getDesktop();
	      if (desktop.isSupported(Action.BROWSE)) {
	        try {
	          desktop.browse(URI.create(url));
	          return;
	        } catch (IOException e) {
	          // handled below
	        }
	      }
	    }
	    
	    // Next try rundll32 (only works on Windows)
	    try {
	      Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
	      return;
	    } catch (IOException e) {
	      // handled below
	    }
	    // Next try the requested browser (e.g. "google-chrome")
	    if (Constant.BROWSER != null) {
	      try {
	        Runtime.getRuntime().exec(new String[] {Constant.BROWSER, url});
	        return;
	      } catch (IOException e) {
	        // handled below
	      }
	    }
	    // Finally just ask user to open in their browser using copy-paste
	    System.out.println("Please open the following URL in your browser:");
	    System.out.println("  " + url);
	}
}
